package com.muyu.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.PageUtils;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.system.Bo.DmShoppingGoodsBo;
import com.muyu.system.Vo.DmShoppingGoodsVo;
import com.muyu.system.Vo.InventoryVO;
import com.muyu.system.domain.DmShoppingGoods;
import com.muyu.system.dto.InventoryDTO;
import com.muyu.system.dto.UpdateInventoryDTO;
import com.muyu.system.service.IDmShoppingGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/goods")
public class DmShoppingGoodsController extends BaseController {
    @Autowired
    private IDmShoppingGoodsService dmShoppingGoodsService;

    /**
     * 查询商品列表
     */
    //@RequiresPermissions("system:goods:list")
    @PostMapping("/list")
    public Result<PageInfo<DmShoppingGoodsVo>> list(@RequestBody DmShoppingGoods dmShoppingGoods) {
        IPage<DmShoppingGoodsVo> page = dmShoppingGoodsService.selectDmShoppingGoodsList(dmShoppingGoods);
        return Result.success(PageUtils.convertToPageInfo(page));
    }

    /**
     * 导出商品列表
     */
    //@RequiresPermissions("system:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DmShoppingGoods dmShoppingGoods) {
        IPage<DmShoppingGoodsVo> page = dmShoppingGoodsService.selectDmShoppingGoodsList(dmShoppingGoods);
        List<DmShoppingGoodsVo> list = page.getRecords();
        List<DmShoppingGoods> dmShoppingGoodsList = BeanUtil.copyToList(list, DmShoppingGoods.class);
        ExcelUtil<DmShoppingGoods> util = new ExcelUtil<DmShoppingGoods>(DmShoppingGoods.class);
        util.exportExcel(response, dmShoppingGoodsList, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    //@RequiresPermissions("system:goods:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        DmShoppingGoods dmShoppingGoods = new DmShoppingGoods();
        dmShoppingGoods.setId(id);
        //List<DmShoppingGoodsVo> dmShoppingGoodsVo = dmShoppingGoodsService.selectDmShoppingGoodsList(dmShoppingGoods);
        DmShoppingGoodsVo dmShoppingGoodsVo = dmShoppingGoodsService.selectDmShoppingGoodsById(id);
        return Result.success(dmShoppingGoodsVo);
    }

    /**
     * 新增商品
     */
    //@RequiresPermissions("system:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody @Valid DmShoppingGoodsBo dmShoppingGoodsBo) {
        return Result.success(dmShoppingGoodsService.insertDmShoppingGoods(dmShoppingGoodsBo), "新增商品成功");
    }

    /**
     * 修改商品
     */
    //@RequiresPermissions("system:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DmShoppingGoodsBo dmShoppingGoodsBo) {
        return Result.success(dmShoppingGoodsService.updateDmShoppingGoods(dmShoppingGoodsBo), "修改商品成功");
    }

    /**
     * 删除商品
     */
    //@RequiresPermissions("system:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return dmShoppingGoodsService.deleteDmShoppingGoodsByIds(ids);
    }

    /**
     * 商品价格排序
     */
    @PostMapping("priceSort")
    public Result selectPriceSort(@RequestBody DmShoppingGoods dmShoppingGoods) {
        List<DmShoppingGoods> list = dmShoppingGoodsService.selectPriceSort(dmShoppingGoods);
        return Result.success(list);
    }


    /**
     * 库存查询
     * @param inventoryDTO
     * @return
     */
    @PostMapping("/inventory")
    public Result<PageInfo<InventoryVO>> inventory(@RequestBody InventoryDTO inventoryDTO){
        return Result.success(PageUtils.convertToPageInfo(dmShoppingGoodsService.inventory(inventoryDTO)));
    }

    /**
     * 修改库存
     * @param list
     * @return
     */
    @PostMapping("/update/inventory")
    public Result<String> updateInventory(@RequestBody List<UpdateInventoryDTO> list){
        dmShoppingGoodsService.updateInventory(list);
        return Result.success();
    }







}
